<?php
/* This file is part of the UARX PHP5 library <http://www.uarx.net/php5lib>.
 * Copyright (C) 2011 UARX Networks.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** 
 * @file   dbConnection.php
 * @brief  UARX PHP5 library file that contains a class definition for a
 *         Database Abstraction using Lazy connections.
 * @date   Mar 28, 2011
 * @author Andres Villa <avilla@uarx.com>
 */

/**
 * @class dbConnection
 * @brief Implements a DB connection class using a Lazy Connection method.
 */
class dbConnection
{
/*! @publicsection */

	/**
	 * Constructor of Database Abstraction using Lazy connections.
	 */
	public function __construct()
	{
		// Set connection state as non connected (false)
		$this->p_connected = false;
		// Read configuration file
		include("dbConnection.config.php");
		// Store database server locally
		$this->p_dbserver = $db_host;
		// Store username locally
		$this->p_username = $db_user;
		// Store database password locally
		$this->p_password = $db_pass;
		// Store database name locally
		$this->p_dbname = $db_name;
	}

	/**
	 * Attempt to connect to a database using pre-configured parameters.
	 *
	 * @return          True if the connection was successful, or false if not.
	 */
	public function connect()
	{
		// If there is an active connection
		if($this->is_connected())
		{
			// Return true, because there is a connection...
			return true;
		}
		// Attempt to connect to the database
		if(! $this->p_connection = mysql_connect($this->p_dbserver, $this->p_username, $this->p_password))
		{
			// TODO: raise an error with trigger_error
			echo get_class($this) . "::Connect() Could not connect to the database.<br>";
			// Return false, because we could't connect
			return false;
		}
		else
		{
			// Connection was made. Now try to select desired database name...
			if (! mysql_select_db($this->p_dbname, $this->p_connection))
			{
				// Disconnect from the database server
				mysql_close($this->p_connection);
				// Set the connection status to non connected again
				$this->p_connection = false;
				// TODO: raise an error with trigger_error
				echo get_class($this) . "::Connect() Could not select the specified database.<br>";
				// Return false, because we could't connect
				return false;
			}
			else
			{
				// Return true, because we are now connected!
				return true;
			}
		}
	}

	/**
	 * Attempt to close the current connection to the database.
	 *
	 * @return           True if it was disconnected, or false if an error
	 *                   occurs.
	 */
	public function close()
	{
		// If a connection exists
		if($this->is_connected())
		{
			if(! mysql_close($this->p_connection))
			{
				// TODO: raise an error with trigger_error
				echo get_class($this) . "::Close() An error occurred when trying to close the connection with the database.<br>";
				// Return false, because we couldn't disconnect.
				return false;
			}
			else
			{
				// Return true, because we've disconnected successfuly
				return true;
			}
		}
		else
		{
			// Nothing to do, return true
			return true;
		}
	}

	/**
	 * Attempt to execute an SQL query into the database.
	 *
	 * @param sql       SQL statement to execute.
	 * @return          True if the query was executed, or false if not.
	 */
	public function query($sql)
	{
		// Empty any previously stored resultset
		$this->p_resultset = "";
		// Connect to the database is we are not connected
		if(! $this->is_connected())
		{
			$this->connect();
		}
		// Verify that connection exist at this point
		if($this->is_connected())
		{
			// Attempt to execute the query
			if(! $m_result = mysql_query($sql, $this->p_connection))
			{
				// TODO: raise an error with trigger_error
				//trigger_error(self . "::Query() could not be executed");
				echo get_class($this) . "::Query() Could not execute the query.<br>MYSQL Error: " . mysql_error() . "<br>";
				// Return false, because we couldn't execute the query
				return false;
			}
			else
			{
				// Define temporary variables to use
				$m_i = 0;
				// Everything is OK, process the resultset
				while ($m_row = mysql_fetch_array($m_result))
				{
					// Store the results in a multidimensional array
					$this->p_resultset[$m_i++] = $m_row;
				}
				// Free the mysql resultset
				mysql_free_result($m_result);
				// Return true
				return true;
			}
		}
		else
		{
			// We were not connected and were not able to reconnect, so return false
			return false;
		}
	}

	/**
	 * Return the connection status to the database server.
	 *
	 * @return          True if the object is connected, or false if not.
	 */
	public function is_connected()
	{
		// If there is an existing resource associated to the connection is
		// because the connection exists.
		if(is_resource($this->p_connection))
		{
			// return true, because a connection exists
			return true;
		}
		else
		{
			// return false, because a connection doesn't exists
			return false;
		}
	}

	/**
	 * Return the resultset of the last query executed.
	 *
	 * @return          An array containing the result set, or an empty array
	 *                  if query method returned no results.
	 * @sa query
	 */
	public function get_results()
	{
		// Just return what we have stored in our array.
		return $this->p_resultset;
	}

/*! @privatesection */

	// Local private parameters
	private $p_connection;
	private $p_resultset = array();
	private $p_dbserver;
	private $p_username;
	private $p_password;
	private $p_dbname;
}
?>